﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using DataAccess;

namespace Spider
{
    namespace Dygod
    {
        /// <summary>
        /// 主页
        /// </summary>
        public class Home : Spider
        {
            /// <summary>
            /// 构造函数
            /// </summary>
            public Home()
            {
                HomeUrl = CurrentUrl = new Uri("https://www.dygod.net/");
            }

            /// <summary>
            /// 开始抓取工作
            /// <returns>是否成功</returns>
            /// </summary>
            public override bool DoWork()
            {
                Public.WriteLog("----- Dygod Home DoWork -----");
                try
                {
                    Public.WriteLog(CurrentUrl.ToString());
                    string content = Encoding.UTF8.GetString(new WebClient().DownloadData(CurrentUrl));
                    Public.WriteLog("Dygod Home DownloadData success!");
                    DoWorkMovie(content);
                    DoWorkTV(content);
                    //DoWorkCartoon(content);
                    //DoWorkShow(content);
                    return true;

                }
                catch (WebException e)
                {
                    Public.WriteLog("Dygod Home WebException: " + e.ToString());
                }
                catch
                {
                    Public.WriteLog("Dygod Home Exception: unknown！");
                }
                return false;
            }

            /// <summary>
            /// 抓取电影
            /// <param name="content">网页内容</param>
            /// </summary>
            public void DoWorkMovie(string content)
            {
                List<string> urls = new List<string>();
                {
                    Regex regex = new Regex("html/gndy/dyzz/\\d+/\\d+.html");
                    foreach (Match m in regex.Matches(content))
                    {
                        urls.Add(HomeUrl.ToString() + m.Value);
                    }
                }
                {
                    Regex regex = new Regex("html/gndy/jddy/\\d+/\\d+.html");
                    foreach (Match m in regex.Matches(content))
                    {
                        urls.Add(HomeUrl.ToString() + m.Value);
                    }
                }
                // 去除重复元素
                urls = urls.Distinct().ToList<string>();
                foreach (string iter in urls)
                {
                    MovieDetail spider = new MovieDetail();
                    spider.HomeUrl = HomeUrl;
                    spider.CurrentUrl = new Uri(iter);
                    spider.DoWork();
                }
            }

            /// <summary>
            /// 抓取电视剧
            /// <param name="content">网页内容</param>
            /// </summary>
            public void DoWorkTV(string content)
            {
                List<string> urls = new List<string>();
                {
                    Regex regex = new Regex("html/tv/hytv/\\d+/\\d+.html");
                    foreach (Match m in regex.Matches(content))
                    {
                        urls.Add(HomeUrl.ToString() + m.Value);
                    }
                }
                {
                    Regex regex = new Regex("html/tv/oumeitv/\\d+/\\d+.html");
                    foreach (Match m in regex.Matches(content))
                    {
                        urls.Add(HomeUrl.ToString() + m.Value);
                    }
                }
                {
                    Regex regex = new Regex("html/tv/rihantv/\\d+/\\d+.html");
                    foreach (Match m in regex.Matches(content))
                    {
                        urls.Add(HomeUrl.ToString() + m.Value);
                    }
                }
                // 去除重复元素
                urls = urls.Distinct().ToList<string>();
                foreach (string iter in urls)
                {
                    TVDetail spider = new TVDetail();
                    spider.HomeUrl = HomeUrl;
                    spider.CurrentUrl = new Uri(iter);
                    spider.DoWork();
                }
            }

            /// <summary>
            /// 抓取动漫
            /// <param name="content">网页内容</param>
            /// </summary>
            public void DoWorkCartoon(string content)
            {
                List<string> urls = new List<string>();
                {
                    Regex regex = new Regex("html/dongman/new/\\d+/\\d+.html");
                    foreach (Match m in regex.Matches(content))
                    {
                        urls.Add(HomeUrl.ToString() + m.Value);
                    }
                }
                // 去除重复元素
                urls = urls.Distinct().ToList<string>();
                foreach (string iter in urls)
                {
                    CartoonDetail spider = new CartoonDetail();
                    spider.HomeUrl = HomeUrl;
                    spider.CurrentUrl = new Uri(iter);
                    spider.DoWork();
                }
            }

            /// <summary>
            /// 抓取综艺
            /// <param name="content">网页内容</param>
            /// </summary>
            public void DoWorkShow(string content)
            {
                List<string> urls = new List<string>();
                {
                    Regex regex = new Regex("html/zongyi.+?/\\d+.html");
                    foreach (Match m in regex.Matches(content))
                    {
                        urls.Add(HomeUrl.ToString() + m.Value);
                    }
                }
                // 去除重复元素
                urls = urls.Distinct().ToList<string>();
                foreach (string iter in urls)
                {
                    ShowDetail spider = new ShowDetail();
                    spider.HomeUrl = HomeUrl;
                    spider.CurrentUrl = new Uri(iter);
                    spider.DoWork();
                }
            }
        }
    }
}